#include "Geometric/Mesh.hpp"

namespace mif
{

	Mesh::Mesh() :
		mNbVertex(0),
		mNbEdge(0),
		mNbFace(0),
		mVertexList(nullptr),
		mFaceList(nullptr)
	{
		//EMPTY
	}
			
	Mesh::~Mesh(){
		delete [] mVertexList;
		delete [] mFaceList;
	}

	Vertex * Mesh::GetVertex(int pInd) const
	{
		return &mVertexList[pInd];
	}

	Face * Mesh::GetFace(int pInd) const
	{
		return &mFaceList[pInd];
	}
	
	int Mesh::GetNbVertex() const
	{	
		return mNbVertex;
	}
					
	int Mesh::GetNbEdge() const
	{
		return mNbEdge;
	}
	
	int Mesh::GetNbFace() const
	{
		return mNbFace;
	}
	
	void Mesh::SetVertexListLength(int pNbVertex){
		mVertexList = new Vertex[pNbVertex];
		mNbVertex = pNbVertex;
	}

	void Mesh::SetFaceListLength(int pNbFace){
		mNbFace = pNbFace;
		mFaceList = new Face[pNbFace];
	}

	void Mesh::SetVertex(int pInd, Vertex pVertex){
		mVertexList[pInd] = pVertex;
	}

	void Mesh::SetFace(int pInd, int pIndVertexA, int pIndVertexB, int pIndVertexC){
		mFaceList[pInd] = Face(GetVertex(pIndVertexA), 
							   GetVertex(pIndVertexB), 
							   GetVertex(pIndVertexC));
	}
	
	Vertex * Mesh::VertexBegin() const
	{
		return &mVertexList[0];
	}
	
	Vertex * Mesh::VertexEnd() const
	{
		return &mVertexList[mNbVertex]  ;
	}
	
	Face * Mesh::FaceBegin() const
	{
		return &mFaceList[0];
	}
	
	Face * Mesh::FaceEnd() const
	{
		return &mFaceList[mNbFace] ;
	}
	
	void Mesh::Show() const
	{
	
		std::cout << "Vertex List : " << std::endl << std::endl;
		
		for(int i = 0; i < mNbVertex; ++i){
			std::cout << "\t n_" << GetVertex(i)->GetID() << " : " << GetVertex(i)->GetCoord().x 
														  << " ; " << GetVertex(i)->GetCoord().y 
														  << " ; " << GetVertex(i)->GetCoord().z;
														  
			std::cout << "\t Neighbor face : ";
			
			if(GetVertex(i)->GetFace() != nullptr)
				std::cout << GetVertex(i)->GetFace()->GetID();
				
			std::cout << std::endl;
		}
				
		std::cout << "Face List : " << std::endl << std::endl;
		
		for(int i = 0; i < mNbFace; ++i){
			std::cout << "\t n_" << GetFace(i)->GetID() << " : " << GetFace(i)->GetTriangle().a->GetID()
														<< " ; " << GetFace(i)->GetTriangle().b->GetID()
														<< " ; " << GetFace(i)->GetTriangle().c->GetID();
			
			std::cout << "\t\t Neighbor faces : ";
			
			if(GetFace(i)->GetNeighborhood().A != nullptr)
				std::cout << "A(" << GetFace(i)->GetNeighborhood().A->GetID() << ") ";
				
			if(GetFace(i)->GetNeighborhood().B != nullptr)
				std::cout << "B(" << GetFace(i)->GetNeighborhood().B->GetID() << ") ";
												  
			if(GetFace(i)->GetNeighborhood().C != nullptr)
				std::cout << "C(" << GetFace(i)->GetNeighborhood().C->GetID() << ")";
				
			std::cout << std::endl;
		}	
		
	}
	
}
